<?php

class DatabaseDux {
    
	private $conString;
	protected $resultSet;
	private $conn;
	public $rowCount;
    
    public function __construct($config,$ruta = '',$connectNow = true,$mode = 'Read'){
        $this->conn = new COM("ADODB.Connection") or die("Cannot start ADO");
        if($ruta == ''){
            $ruta = $config['host'];
        }
        
        $this->conString = "
            Provider=MSDASQL.1;
            Persist Security Info=False;
            Data Source=" . $config['DSN']. ";
            Mode=$mode;
            Extended Properties='
            DSN=" . $config['DSN']. ";
            DBQ=$ruta;
            Extension=*;
            Oem=N;
            Datefield=%fecha%;
            NullEmptyStr=N;
            NoDot=N;
            ExtSeperator=.;
            UlongAsDate=N;
            ';
            Initial Catalog=(predeterminada)";
        if($connectNow){
                $this->connect();
        }
    }
    
    public function connect(){
        $this->conn->Open($this->conString);
    }

    public function disconnect(){
            $this->conn->close();
    }

    public function setQuery($sql){
        $this->resultSet = $this->conn->execute($sql) or die ($this->conn->error());
    }

    public function fetchArrayListEx(){
        $res = array();
        $rs = &$this->resultSet;
        $rowcount = 0;
        $num_columns = $rs->Fields->Count();
        
        while (!$rs->EOF){
            for ($i=0; $i < $num_columns; $i++) {
                $res[$rowcount][strtolower($rs->Fields($i)->name())] = $rs->Fields($i)->value();
            }
            $rowcount++;            // increments rowcount
            $rs->MoveNext();
        }
        $this->rowCount = $rowcount;
        return $res;
	}

    public function getNumField(){
        return $this->resultSet->Fields->Count();
    }
}

class Database extends PDO {
    
    private $conString;
	protected $resultSet;
	private $conn;
	public $rowCount;

    public function __construct($config){
        parent::__construct(
            'mysql:host=' . $config['host'] .
            ';dbname=' . $config['dbname'] ,
            $config['user'],
            $config['pass'],
            array(pdo::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $config['char'])
        );
    }
    
    public function _query($query){
        return $this->query($query);
    } 
}

class DatabaseSQLServer {
    
    private $server;
    private $user;
    private $pass;
    private $db;
    private $constring;
    protected $resultSet;
    private $conn;
    public $rowCount;
    
    public function __construct($config){
        $this->server = $config['host'];
		$this->db = $config['dbname'];
		$this->user = $config['user'];
		$this->pass = $config['pass'];
        
        $this->constring = "DRIVER=SQL Server;SERVER=" . $this->server . ";DATABASE=" . $this->db;
    }
    
    public function connect(){
        $this->conn = odbc_connect($this->constring, $this->user, $this->pass) or die(odbc_error());
    }

    public function disconnect(){
        odbc_close($this->conn);
    }

    public function executeQuery($sqlString, $sqlParams = null, $errMsg =  'Unkwon Error'){
        if($this->resultSet){
                odbc_free_result($this->resultSet);
        }
        if($sqlParams==null){
        $this->resultSet = odbc_exec($this->conn, $sqlString) or die(odbc_error($this->conn));
        } else {
                $this->resultSet = odbc_prepare($this->conn, $sqlString);
                odbc_execute($this->resultSet, $sqlParams) or die($errMsg);
        }
    }

    public function fetchArrayList(){
        $row = array();
        $rows = array();

        while(odbc_fetch_into($this->resultSet, $row)){
            array_push($rows,$row);
        }

        $this->fetchRowCount($rows);
        return $rows;
    }

    public function fetchArrayListEx(){
        $i = 0 ;
        $j = 0;
        $tmpResult = array();
        while(odbc_fetch_row($this->resultSet)){
            for($j=1; $j<= odbc_num_fields($this->resultSet); $j++){
                $fieldName = odbc_field_name($this->resultSet,$j);
                $ar[$fieldName] = odbc_result($this->resultSet,$fieldName);
            }
            $tmpResult[$i]  = $ar;
            $i++;
        }

        //sets row count property
        $this->fetchRowCount($tmpResult);
        return $tmpResult;
    }
    
    public function getField($table){
        $this->executeQuery("SELECT * FROM " . $table);
        for($j=1; $j<= odbc_num_fields($this->resultSet); $j++){
			$fieldName[] = odbc_field_name($this->resultSet,$j);
		}
        return $fieldName;
    }

    private function fetchRowCount($arrCount ){
        if(is_array($arrCount)){
                $this->rowCount = sizeof($arrCount);
        }
    }

    public function printOut($obj){
        echo "<pre>";
        print_r($obj);
        echo "</pre>";
    }
}

abstract class DB {
    
    abstract function connect($param);
    abstract function disconnect();
    abstract function setQuery($sql);
    abstract function listArray();
    abstract function rowArray();
    
}

class DUX_DB extends DB{
    
    public function __construct(){
        parent::__construct(new COM("ADODB.Connection"));
    }
    
    function connect($param){
        $this->setParam($param);
        
        $this->conn->Open("
            Provider=MSDASQL.1;
            Persist Security Info=False;
            Data Source=" . $this->_DSN . ";
            Mode=" . $this->_mode . ";
            Extended Properties='
            DSN=" . $this->_DSN . ";
            DBQ=" . $this->_ruta . ";
            Extension=*;
            Oem=N;
            Datefield=%fecha%;
            NullEmptyStr=N;
            NoDot=N;
            ExtSeperator=.;
            UlongAsDate=N;
            ';
            Initial Catalog=(predeterminada)
        ");
        
        $this->open = true;
    }
    
    function disconnect(){}
    
    function setQuery($sql){
        $this->resultSet = $this->conn->execute($sql) or die ($this->conn->error());
    }
    
    function listArray(){}
    
    function rowArray(){}
}

class MYSQL_DB extends DB {
    
    public function __construct($param = array('open' => true)){
        parent::__construct(new mysqli);
        
        $this->param = $param;
        
        if(isset($param['open']) && $param['open']){
            $this->open();
        }
    }
    
    function connect($param){}
    function disconnect(){}
    function setQuery($sql){}
    function listArray(){}
    function rowArray(){}
}

class MSSQL_DB extends DB {
    
    public function __construct($param = array('open' => true)){
        $this->param = $param;
        
        if(isset($param['open']) && $param['open']){
            $this->open();
        }
    }
    
    function connect($param){}
    function disconnect(){}
    function setQuery($sql){}
    function listArray(){}
    function rowArray(){}
}
?>